<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\Staff;
use think\Controller;

class UcenterController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 60000  修改失败
         * 60001  查询失败
         * 60002  修改失败
         * 60003  删除失败
         * 
         */
    }

    /*
     * 修改昵称
     * author xfwed
     * date 2019.12.05
     */

    public function saveNickname() {
        $rule = [
            'staff_id' => 'require',
            'nickname' => 'require',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
            'nickname.require' => '昵称必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id']; //美容师id
        $nickname = $all['nickname']; //昵称
        $save = Staff::where('id', $staff_id)->update(['nickname' => $nickname]);
        if ($save) {
            return Utils::ajaxReturn(0, '修改成功', null);
        } else {
            return Utils::ajaxReturn(60000, '修改失败', null);
        }
    }

    /*
     * 我的认证
     * author xfwed
     * date 2019.12.05
     */

    public function myCert() {
        $rule = [
            'staff_id' => 'require',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id']; //美容师id
        $row = Staff::where('id', $staff_id)->field('cert_img,health_img')->find();
        if ($row) {
            return Utils::ajaxReturn(0, '查询成功', $row);
        } else {
            return Utils::ajaxReturn(60001, '查询失败', null);
        }
    }

    /*
     * 修改认证
     * author xfwed
     * date 2019.12.05
     */

    public function saveMyCert() {
        $rule = [
            'staff_id' => 'require',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id']; //美容师id
        $cert_img = input('param.cert_img'); //美容师资格证
        $health_img = input('param.health_img'); //美容师健康证
        $row = Staff::where('id', $staff_id)->field('cert_img,health_img')->find();
        if (empty($cert_img)) {
            $cert_img = $row['cert_img'];
        }  else {//上传图片
            $IndexPath = config('appconfig.IndexPath');
            $postFields['modeName'] = 'staff';
            $postFields['img'] = $cert_img;
            $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
            $img = json_decode($img,TRUE);
            $cert_img = $img['data'];
        }
        if (empty($health_img)) {
            $health_img = $row['health_img'];
        }  else {//上传图片
            $IndexPath = config('appconfig.IndexPath');
            $postFields['modeName'] = 'staff';
            $postFields['img'] = $health_img;
            $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
            $img = json_decode($img,TRUE);
            $health_img = $img['data'];
        }
        $save = Staff::where('id', $staff_id)->update(['cert_img'=>$cert_img,'health_img'=>$health_img]);
        $returnData['cert_img'] = $cert_img;
        $returnData['health_img'] = $health_img;
        if ($save) {
            return Utils::ajaxReturn(0, '修改成功', $returnData);
        } else {
            return Utils::ajaxReturn(60002, '修改失败', null);
        }
    }
    
    /*
     * 删除认证
     * author xfwed
     * date 2019.12.06
     */

    public function delMyCert() {
        $rule = [
            'staff_id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
            'type.require' => '类型必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id']; //美容师id
        $type = $all['type']; //1资格证 2健康证
        if($type == 1){
            $save = Staff::where('id',$staff_id)->update(['cert_img'=>NULL]);
        }elseif ($type == 2) {
            $save = Staff::where('id',$staff_id)->update(['health_img'=>NULL]);
        }
        if ($save) {
            return Utils::ajaxReturn(0, '删除成功', null);
        } else {
            return Utils::ajaxReturn(60003, '删除失败', null);
        }
    }

}
